package com.ruoyi.controller;


import java.awt.image.RenderedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;

import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.OpenCVFrameConverter;
import org.bytedeco.javacv.OpenCVFrameGrabber;
import org.bytedeco.opencv.opencv_core.IplImage;
import org.bytedeco.opencv.opencv_core.Mat;

import org.bytedeco.opencv.opencv_videoio.VideoCapture;

import org.springframework.stereotype.Component;


import javax.imageio.ImageIO;
import java.io.ByteArrayOutputStream;


import static org.opencv.videoio.Videoio.CAP_DSHOW;

@Component
public class Camera {
    public byte[] startStream() throws Exception {
        OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
        grabber.start();

        while (true) {
            Frame frame = grabber.grab();
            // 这里可以对frame进行进一步处理，例如缩放或裁剪

            // 将frame转换成图像数据流
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage();
            IplImage image = converter.convert(frame);
            // 这里可以对IplImage进行进一步处理，例如旋转或添加水印

            // 将图片数据写入输出流
            OpenCVFrameConverter.ToMat matConverter = new OpenCVFrameConverter.ToMat();
            Mat mat = matConverter.convert(frame);
            ImageIO.write((RenderedImage) matConverter.convert(mat), "jpg", outputStream);

            System.out.println( mat);
            // 返回图像数据流
            byte[] imageBytes = outputStream.toByteArray();

            return imageBytes;
        }
    }


}



//
//package com.ruoyi.controller;
//
//import org.bytedeco.javacv.OpenCVFrameConverter;
//import org.bytedeco.opencv.opencv_core.IplImage;
//import org.bytedeco.opencv.opencv_core.Mat;
//import org.bytedeco.opencv.opencv_videoio.VideoCapture;
//import org.springframework.stereotype.Component;
//
//import javax.imageio.ImageIO;
//import java.awt.image.RenderedImage;
//import java.io.ByteArrayOutputStream;
//import java.io.IOException;
//import java.util.Base64;
//
//import static org.opencv.videoio.Videoio.CAP_DSHOW;
//
//@Component
//public class Camera {
//    public String startStream() throws Exception {
//        try (VideoCapture capture = new VideoCapture(0,CAP_DSHOW)) {
////            capture.set(VideoCapture.CAP_PROP_FRAME_WIDTH, 640);
//            capture.set(VideoCapture.CAP_PROP_FRAME_HEIGHT, 480);
//            capture.set(VideoCapture.CAP_PROP_FPS, 30);
//            capture.set(VideoCapture.CAP_PROP_FOURCC, VideoCapture.CAP_PROP_FOURCC_DEFAULT);
//            capture.start();
//
//            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
//            while (true) {
//                Mat frame = new Mat();
//                if (capture.read(frame)) {
//                    // 这里可以对frame进行进一步处理，例如缩放或裁剪
//
//                    // 将frame转换成图像数据流
//                    ByteArrayOutputStream tempOutputStream = new ByteArrayOutputStream();
//                    ImageIO.write((RenderedImage) OpenCVFrameConverter.ToMat().convert(frame), "jpg", tempOutputStream);
//
//                    // 返回图像数据流
//                    byte[] imageBytes = tempOutputStream.toByteArray();
//                    String base64Image = Base64.getEncoder().encodeToString(imageBytes);
//
//                    return base64Image;
//                }
//            }
//        }
//    }
//}